package com.qhit.controller;

import com.qhit.pojo.Goods;
import com.qhit.pojo.Tourist;
import com.qhit.service.TouristService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Controller
@RequestMapping("/tourist")
public class TouristController {
    @Autowired
    private TouristService touristService;

    @RequestMapping("/list")
    public String list(Model model,@RequestParam String keywords,
                       @RequestParam(defaultValue = "1") Integer pageSize,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "1") Integer pageNo){
        HashMap hashMap = new HashMap<>();
        hashMap.put("keywords",keywords);
        hashMap.put("pageSize",pageSize);
        hashMap.put("page",page);
        hashMap.put("pageNo",pageNo);
        long total=touristService.getListCount(hashMap);
        System.out.println(total);
        hashMap.put("totalPage",(total%pageSize==0?(total/pageSize):((total/pageSize)+1)));
        List<Tourist> list =  touristService.getList(hashMap);
        model.addAttribute("list",list);
        model.addAttribute("myParams",hashMap);
        return "tourist/list";
    }
//   @RequestMapping("/list2")
//    public String list2(Model model){
//        List<Goods> list =  goodsService.getList2();
//        model.addAttribute("list",list);
//        return "goods/list";
//    }
    @RequestMapping("/toAdd")
    public String toAdd (){
        return "tourist/add";
    }
    @PostMapping("/add")
    public String add ( Tourist tourist , Model model){
        tourist.setId(UUID.randomUUID().toString());
        int i = touristService.add(tourist);
        if(i>0){
            model.addAttribute("msg","添加成功");
        }else {
            model.addAttribute("msg","添加失败");
        }
        return "tourist/msg";
    }
    @RequestMapping("/toEdit/{id}")
    public String toEdit (@PathVariable("id") String id,Model model){
        Tourist tourist=touristService.getTouristById(id);
        System.out.println(tourist);
        model.addAttribute("tourist",tourist);
        return "tourist/edit";
    }
    @RequestMapping("/delete/{id}")
    public String delete (@PathVariable("id") String id,Model model){
        int i=touristService.delete(id);
        if(i>0){
            model.addAttribute("msg","删除成功");
        }else {
            model.addAttribute("msg","删除成功");
        }
        return "tourist/msg";
    }
    @PostMapping("/edit")
    public String edit (Tourist tourist, Model model){

        int i = touristService.edit(tourist);

        if(i>0){
            model.addAttribute("msg","修改成功");
        }else {
            model.addAttribute("msg","修改失败");
        }
        return "tourist/msg";
    }
}
